package can.bishe.modules.exam.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import can.bishe.common.validator.ValidatorUtils;
import can.bishe.modules.sys.controller.AbstractController;
import can.bishe.modules.sys.entity.SysUserEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import can.bishe.modules.exam.entity.ExamScoreEntity;
import can.bishe.modules.exam.service.ExamScoreService;
import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.R;



/**
 * 分数信息
 *
 * @author can
 * @email 822024417@qq.com
 * @date 2021-04-28 23:03:48
 */
@RestController
@RequestMapping("exam/examscore")
public class ExamScoreController extends AbstractController {
    @Autowired
    private ExamScoreService examScoreService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity user = getUser();
        PageUtils page = examScoreService.queryPage(params,user);

        return R.ok().put("page", page);
    }

    @RequestMapping("/myList")
    public R myList(@RequestParam Map<String, Object> params){

        Long userId = getUserId();
        PageUtils page = examScoreService.queryMyPage(params,userId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{resId}")
    public R info(@PathVariable("resId") Long resId){
        ExamScoreEntity examScore = examScoreService.getById(resId);

        return R.ok().put("examScore", examScore);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamScoreEntity examScore){
        examScoreService.save(examScore);

        return R.ok();
    }

    /**
     * 交卷
     */
    @RequestMapping("/submitOver")
    public R submitOver(@RequestBody ExamScoreEntity examScore){
        Long userId = getUserId();
        examScore.setUserId(userId);
        examScore.setExamTime(new Date());
        // 如果有成绩则更新
        ExamScoreEntity examScoreEntity = examScoreService.getOne(new QueryWrapper<ExamScoreEntity>().eq("exam_title",examScore.getExamTitle()));
        if( examScoreEntity != null ){
            examScore.setResId(examScoreEntity.getResId());
            examScoreService.updateById(examScore);
            return R.ok();
        }
        examScoreService.save(examScore);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExamScoreEntity examScore){
        ValidatorUtils.validateEntity(examScore);
        examScoreService.updateById(examScore);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] resIds){
        examScoreService.removeByIds(Arrays.asList(resIds));

        return R.ok();
    }

}
